package com.lxk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lxk.pojo.ConsumeOrder;
import com.lxk.pojo.Rules;
import com.lxk.pojo.Users;
import com.lxk.redis.RedisKey;
import com.lxk.redis.RedisUtil;
import com.lxk.service.ConsumeOrderService;
import com.lxk.service.PlayService;
import com.lxk.service.UsersService;
import com.lxk.vo.PageModel;
import com.lxk.vo.ScanResultVO;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JohnSoybean
 * @create 2020/7/15 11:28
 * @description 游戏扫码交互等控制器
 */
@RestController
@RequestMapping("/play")
public class PlayController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private PlayService playService;
    @Autowired
    private ConsumeOrderService consumeOrderService;
    @Autowired
    private UsersService usersService;

    /**
     * 获取规则
     * @param
     * @return
     */
    @GetMapping("/rule")
    public Result<Rules> getRules() {
        Rules rule = playService.getRule();
        return Result.success(rule);
    }

    /**
     * 获取二维码
     * @param request
     * @return
     */
    @GetMapping("/qr")
    public Result<String> getUserQrCode(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String qrCode = playService.getQRCode(userId);
        return Result.success(qrCode);
    }

    /**
     * 二维码轮询
     * @param request
     * @return
     */
    @GetMapping("/polling")
    public Result<ScanResultVO> pollingQrCode(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        Users users = usersService.getById(userId);
        if (users == null) {
            return Result.error(CodeMsg.USER_NOT_EXIST);
        }
        String uuid = users.getUuid();
        String resJson = (String) redisUtil.get(redisUtil.joinKey(RedisKey.QRCODE.getPrefix(), uuid));
        ObjectMapper om = new ObjectMapper();
        try {
            ScanResultVO vo = om.readValue(resJson, ScanResultVO.class);
            return Result.success(vo);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return Result.error("轮询数据获取失败");
        }
    }

    /**
     * 棒球机消费记录
     *
     * @return
     */
    @GetMapping("/orders")
    public Result<PageModel<ConsumeOrder>> showConsumeOrderList(HttpServletRequest request, Integer pageNum, Integer pageSize) {
        Long userId = (Long) request.getAttribute("userId");
        Page<ConsumeOrder> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ConsumeOrder> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("consume_order_id");
        wrapper.eq("user_id", userId);
        consumeOrderService.page(page, wrapper);
        PageModel<ConsumeOrder> model = new PageModel<>(page);
        return Result.success(model);
    }

}
